package kk.service.impl;

import com.github.pagehelper.PageHelper;
import kk.dao.UserDao;
import kk.domain.Role;
import kk.domain.UserInfo;
import kk.service.UserService;
import kk.utils.PasswordUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;


@Service("userService")
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        UserInfo userInfo = null;
        try {
            //从数据库查询出用户
            userInfo = userDao.findByUsername(username);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //处理自己的用户对象封装成UserDetails
        User user = new User(userInfo.getUsername(), userInfo.getPassword(), userInfo.getStatus() == 0 ? false : true, true, true, true,getAuthority(userInfo.getRoles()));
        return user;
    }

    /**
     * 获取用户的角色信息，并封装成SimpleGrantedAuthority的集合
     * @param roles
     * @return
     */
    public List<SimpleGrantedAuthority> getAuthority(List<Role> roles){
        List<SimpleGrantedAuthority> list=new ArrayList<>();
        for (Role role : roles) {
            list.add(new SimpleGrantedAuthority("ROLE_"+role.getRoleName()));
        }
        return list;
    }


    
    @Override
    public List<UserInfo> findAll(Integer pageNum,Integer pageSize) throws Exception {
        PageHelper.startPage(pageNum,pageSize);
        List<UserInfo> userInfoList = userDao.findAll();
        return userInfoList;
    }

    @Override
    public void save(UserInfo userInfo) throws Exception {
        String encodePassword = PasswordUtils.EncodePassword(userInfo.getPassword());
        //登录密码进行加密并且保存到数据库中
        userInfo.setPassword(encodePassword);
        userDao.save(userInfo);
    }

    @Override
    public UserInfo findById(Integer id) throws Exception {
        UserInfo userInfo = userDao.findById(id);
        return userInfo;
    }

    @Override
    public List<Role> findOtherRoles(Integer uid) {
        List<Role> otherRoles = userDao.findOtherRoles(uid);
        return otherRoles;
    }

    @Override
    public void addRoleToUser(Integer userId, int[] ids) throws Exception {
        for (int roleId : ids) {
            userDao.addRoleToUser(userId,roleId);
        }
    }

    /**
     * 根据用户的Id删除用户
     * @param id
     * @throws Exception
     */
    @Override
    public void deleteById(Integer id) throws Exception {
        //  先从users_role表中删除包含用户id的记录
        userDao.deleteFromUsers_roleById(id);
        userDao.deleteById(id);
    }

    @Override
    public List<Role> findExistRoles(Integer userId) throws Exception {
        List<Role> existRoles = userDao.findExistRoles(userId);
        return existRoles;
    }

    @Override
    public void deleteRoleFromUser(Integer userId, int[] ids) throws Exception {
        for (int id : ids) {
            userDao.deleteRoleFromUser(userId,id);
        }
    }
}
